﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RedBlueBalls
{
    class RedContinual
    {
        private static bool analyzed = false;
        private static bool checkBest = false;
        private const int RED_BALL = 6;
        private const int DEPTH = 19;
        private const int BEST = 12; // 65%
        private static int lastContinuals = 0; 

        public static void analyze(RedBlueBallRecord[] records)
        {
            if (checkBest)
            {
                int count = 0;
                foreach (RedBlueBallRecord record in records)
                {
                    if (IsContinual(record.GetReds()))
                        count++;
                }

                Tracer.AppendMessage(String.Format("Continual {0} / Total {1}", count, records.Length));

            }

            lastContinuals = 0;

            for (int i = 0; i < DEPTH; i++)
            {
                if (IsContinual(records[records.Length - 1 - i].GetReds()))
                    lastContinuals++;
            }

            analyzed = true;
        }

        public static bool predict(RedBlueBallRecord candidate)
        {
            if (!analyzed)
            {
                Tracer.AppendMessage("Please analyze the data first!");
                return false;
            }

            bool isContinual = IsContinual(candidate.GetReds());

            if (lastContinuals > BEST)
            {
                if (isContinual)
                    return false;
            }
            else if (!isContinual)
                return false;

            return true;
        }

        private static bool IsContinual(int[] reds)
        {
            for (int i = 1; i < RED_BALL; i++)
                if (reds[i] - reds[i - 1] == 1)
                    return true;

            return false;
        }
    }
}
